/*
 * @Descripttion: 
 * @Mender: zhtao
 * @Date: 2020-05-21 18:45:53
 * @LastEditors: zhtao
 * @LastEditTime: 2020-06-04 10:58:08
 */
import * as API_Members from '../../api/members'
import * as API_Trade from '../../api/trade'
import * as API_Goods from '../../api/goods'

import {
  Foundation,
  RegExp
} from '../../ui-utils/index'
let util = require('../../utils/util.js')
const App = getApp()

Page({
  data: {
    activeMember: 0,
    finished: false,
    show: false,
    from: '', // 页面涞源
    publicVip: [],
    phone: '',
    price: 9.99,
    cost:1,
    //会员级别信息
    publicMemberGrade:"",
    grade:'1',
    isShow:false
  },
  onLoad(options) { // 设置页面来源
    this.setData({
      from: options.from || ''
    })
    // if(wx.getStorageSync('inviteCode')){
    //   const referrer_mobile=wx.getStorageSync('inviteCode')
    //   this.setData({phone:referrer_mobile})  
    // }
    // if(wx.getStorageSync('qrId')){
    //   const referrer_mobile=wx.getStorageSync('inviteCode')
    //   this.setData({phone:referrer_mobile})        
    // }
  
  },
  onShow() {
    wx.showLoading({
      title: '加载中',
    })
    /* 获取用户公域等级信息*/
    API_Members.getPublicMemberGrade().then(res=>{
      console.log('res',res);
      
      wx.hideLoading()
      this.setData({
        publicMemberGrade:res.grade
      })
      console.log('this.data.publicMemberGrade',this.data.publicMemberGrade);
      console.log('this.data.grade',this.data.grade);
      
      if(this.data.grade<=this.data.publicMemberGrade){
        this.setData({
          isShow:true
        })
      }if(this.data.grade>this.data.publicMemberGrade){
        this.setData({
          isShow:false
        })
      }
      if(this.data.grade === null){
        this.setData({isShow:false})
      }
    })
    
    
    this.getPublicMember()
    /* 公域上级 */
    API_Members.getPublicSuperior().then(res => {
      console.log(res)
      if (res.mobile) {
        this.setData({
          phone: res.mobile
        })
      } else {
        /* 推广上级 */
        API_Members.getRecommendMe().then(data => {
          console.log(data);
          this.setData({
            phone: data.mobile || ""
          })
        });
      }
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  buyvip() {
    const { phone } = this.data;
    if (phone == wx.getStorageSync('user').mobile) {
      wx.showToast({ title: '不能填写自己的手机号', icon: 'none' })
      return
    }
    if (phone != "" && !/^1[3456789]\d{9}$/.test(phone)) {
      wx.showToast({ title: "邀请码格式不正确！", icon: "none" });
      return;
    }
    if (phone != "" && /^1[3456789]\d{9}$/.test(phone)) {
      API_Members.MobileCheck(phone).then(data => {
        if (!data.exist) {
          wx.showToast({ title: '请确保输入的手机已注册', icon: 'none' })
          return;
        } else {
          this.buyorder();
        }
      });
    }
    if(phone==''){
      // this.setData({phone:null})
    this.buyorder();
    }
  },
  buyorder() {
    API_Trade.createVip({
      client: "WAP",
      mobile: this.data.phone,
      vipId: this.data.activeMember
    }).then(data => {
      wx.redirectTo({ url: `/pages/cashier/cashier?trade_sn=${data.trade_sn}` })
    });
  },
  opendialog() {
    let selected = this.data.publicVip.find(item => item.id == this.data.activeMember)
    this.setData({
      show: true
    })
    console.log(selected)
  },
  selectvip(e) {
    this.getPrice(e.currentTarget.dataset.selectid);
    this.setData({
      activeMember: e.currentTarget.dataset.selectid,
      grade:e.currentTarget.dataset.grade
    })
    if(this.data.grade>this.data.publicMemberGrade){
        this.setData({
          isShow:false
        })
    }else{
      this.setData({
        isShow:true
      })
    }
  },
  /* 获取购买会员价格 */
  getPrice(id) {
    API_Goods.getVipPrice(id).then(data => {
      // console.log('getVipPrice', data);
      this.setData({
        price:Foundation.formatPrice(data.price),
        cost:Foundation.formatPrice(data.cost)
      })   
      // this.setData({ vip_price: Foundation.formatPrice(data) })
    })
  },
  getPublicMember() {
    API_Members.getPublicMember().then((response) => {
      let data = response.data
      data.map((item, index) => {
        item.icon = 'http://img.oupinguoji.cn/icon' + (index + 1) + '.png'
        item.a_icon = 'http://img.oupinguoji.cn/icon' + (index + 1) + '_i.png'
        
      })
      //console.log(data)
      this.getPrice(data[0].id);
      this.setData({
        activeMember: data[0].id,
        publicVip: data,
        
      })
    })
  },

})